export default function (context) {
    let user = {username:"123456",password:"123456"};


    context.naive.addLogic("helloWorld",async(ctx,pck)=>{
        let helloNaive = ctx.Naive();
        helloNaive.method("redirect",async(ctx,pck)=>{
            await ctx.invoker.interact("redirect",{url:"hello.redirect.html"});
            return;
        });
        helloNaive.method("popup",async(ctx,pck)=>{
            await ctx.invoker.interact("popup",{url:"hello.popup.html"});
            return;
        });
        helloNaive.method("info",async(ctx,pck)=>{
            await ctx.invoker.interact("info","helloWorld");
            return;
        });
        helloNaive.method("input",async(ctx,pck)=>{
            let input = await ctx.invoker.interact("input",{url:"hello.input.html"});
            console.log(input);
            return;
        });
        helloNaive.method("store",async(ctx,pck)=>{
            await ctx.invoker.interact("store",{name:"张三",age:12});
            return;
        });
        helloNaive.method("finish",async(ctx,pck)=>{
            return "手机展示的东西";
        });
        helloNaive.method("retrieve",async(ctx,pck)=>{
            let {name,age} = await ctx.invoker.interact("store",["name","age"]);
            console.log("name",name);
            console.log("age",age);
            return;
        });
        helloNaive.method("forword",async(ctx,pck)=>{
            let flag = await ctx.forward("forword_example");
            return flag;
        });
        await ctx.invoker.interact("finish",{naive:helloNaive.address});
        let finish = await helloNaive.carry("finish");
        if (finish){
            return "结束教程";
        }
    });

    context.naive.addLogic("forword_example",async(ctx,pck)=>{
        return "这里是forword的返回值";
    });




    context.naive.addLogic("begin", async (ctx, pck) => {
        let beginNaive = ctx.Naive();

        let result = await ctx.invoker.interact("input",{url:"begin.input.xml"});
        console.log(`收到从begin.input.xml页面返回的数据了:${result}`);

        await ctx.invoker.interact("info",`这是begin方法info回去的字符串`);


        await ctx.invoker.interact("store",{name:"begin-张三",age:"begin-18岁"});
        console.log(`客户端已存好这些键值对`);

        let retrieve = await ctx.invoker.interact("retrieve",["name","age"]);
        console.log(`收到手机存在本地数据库里的${JSON.stringify(retrieve)}`);

        let user = await ctx.forward("login");
        console.log(`执行完forword后得到user值${JSON.stringify(user)}`);
        return user;//等价于ctx.invoker.interact('finish',user);
    });
	
    context.naive.addLogic("login",async(ctx,pck)=>{
        let loginNaive = ctx.Naive();
        loginNaive.method("back",async(ctx,pck)=>{
            console.log("客户端退出了begin.redirect.xml页面");
            return false;
        });
        loginNaive.method("ok",async(ctx,pck)=>{
            let {name} = pck;
            if(name !== "张三"){
                throw "名字不等于张三,请重新输入"
            }
            console.log("客户端完成了数据验证,继续流程");
            return {name:pck.name};
        });
        await ctx.invoker.interact("redirect",{url:"begin.redirect.xml",naive:loginNaive.address});
        console.log(`等待客户端退出当前页面,或者完成操作`);
        let redirect = await loginNaive.carry("back","ok");
        if(redirect){
            return {name:redirect.name}
        }else {
            throw "";
        }
    });








    //
    //
    // context.naive.addLogic("login", async (ctx, pck) => {
    //     let loginNaive = ctx.Naive();
    //     loginNaive.method("login", async (ctx, pck) => {
    //         if (user.username === pck.username && user.password === pck.password){
    //             return user;
    //         }
    //         throw "账号密码不匹配";
    //     });
    //     loginNaive.method("register", async (ctx, pck) => {
    //         await ctx.invoker.interact("redirect",{url:'register.html'});
    //         await ctx.invoker.interact("info",`注册成功,用户名为${user.username},密码为${user.password}`);
    //         return user;
    //     });
    //     loginNaive.method("forget", async (ctx, pck) => {
    //
    //     });
    //     loginNaive.method("close",async(ctx,pck)=>{
    //         return false;
    //     });
    //     await ctx.invoker.interact("redirect", {
    //             url: "login.html",
    //             data: {
    //                 welcome: "欢迎"
    //             },
    //             naive: loginNaive.address
    //         }
    //     );
    //     let isLogin = await loginNaive.carry("login", "register","close");
    //     if (!isLogin){
    //         throw "";
    //     }
    //     return isLogin;
    // });
    //


    return {};
};

